package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("role")
public class RoleController {
	@Autowired
	IRoleService service;

	@RequestMapping("")
	public String index(){
		return "role/list";
	}

	@RequestMapping("list")
	@ResponseBody
	public List<Role> queryDepts(){
		return service.selectAll();
	}

	@RequestMapping("save")
	@ResponseBody
	public JsonResult save(Role role){
		JsonResult result = new JsonResult();
		try {
			service.insert(role);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("操作失败");
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if(id!=null){
				service.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("操作失败");
		}
		return result;
	}

	@RequestMapping("update")
	@ResponseBody
	public JsonResult update(Role role){
		JsonResult result = new JsonResult();
		try {
			service.updateByPrimaryKey(role);
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("操作失败");
		}
		return result;
	}
}
